﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Cwing.UI.Validation;
using System.Web;
using System.Web.Configuration;
using System.Reflection;

namespace Cwing.UI
{
    public sealed class ValidationConfigHelper
    {
        public static ValidationSection section;

        static XmlDocument xmldoc = null;

        static ConfigFileProvider provider = null;

        static ValidationConfigHelper()
        {
            section = WebConfigurationManager.GetSection("ValidationProvider") as ValidationSection;
            if (section != null && !String.IsNullOrEmpty(section.Provider))
            {
                String[] buffer = section.Provider.Split(',');
                if (buffer.Length == 2)
                {
                    Assembly assembly = Assembly.Load(buffer[1]);
                    provider = assembly.CreateInstance(buffer[0]) as ConfigFileProvider;
                }
            }

            if (provider == null)
                provider = new DefaultProvider();
        }

        public static Configuration FetchConfigNode(String xpath)
        {

            xmldoc = provider.LoadFile();

            if (String.IsNullOrEmpty(xpath))
            {
                return null;
            }

            Configuration config = new Configuration();
            XmlNodeList nodes = xmldoc.SelectNodes(xpath);
            XmlNode node = null;
            if (nodes != null && nodes.Count > 0)
            {
                node = nodes[0];
            }

            if (node == null)
            {
                return config;
            }
            else
            {
                //Mandatory
                XmlNode mandatoryNode = node.SelectSingleNode("Mandatory");
                if (mandatoryNode != null)
                {
                    Boolean mandatory = false;
                    XmlAttribute mandatoryAttr = mandatoryNode.Attributes["Value"];
                    if (mandatoryAttr != null)
                    {
                        Boolean.TryParse(mandatoryAttr.Value, out mandatory);
                    }
                    config.Mandatory = mandatory;
                    config.MandatoryMessage = mandatoryNode.InnerText;
                }

                //Length Config
                XmlNode length = node.SelectSingleNode("Length");
                if (length != null)
                {
                    XmlAttribute minLength = length.Attributes["MinLength"];
                    if (minLength != null)
                    {
                        Int32 min = config.MinLength;
                        Int32.TryParse(minLength.Value, out min);
                        config.MinLength = min;
                    }

                    if (config.MinLength < 0)
                    {
                        config.MinLength = 0;
                    }

                    XmlAttribute maxLength = length.Attributes["MaxLength"];
                    if (maxLength != null)
                    {
                        Int32 max = config.MaxLength;
                        Int32.TryParse(maxLength.Value, out max);
                        config.MaxLength = max;
                    }
                    if (config.MaxLength < 0)
                    {
                        config.MaxLength = 0;
                    }
                    config.LengthMessage = length.InnerText;
                }

                XmlNode type = node.SelectSingleNode("Type");
                if (type != null)
                {
                    XmlAttribute regEx = type.Attributes["Regex"];
                    if (regEx != null)
                    {
                        config.TypeRegEx = regEx.Value;
                    }
                    config.TypeMessage = type.InnerText;
                }
                return config;
            }
        }

        public static String MandatoryMessage()
        {
            if (xmldoc == null)
            {
                return String.Empty;
            }
            else
            {
                XmlNode node = xmldoc.SelectSingleNode("/Site/Mandatory");
                if (node == null)
                {
                    return String.Empty;
                }
                else
                {
                    return node.InnerText;
                }
            }
        }
    }
}
